const deleteImage = require("../../upload/controller/delete")
const delete_article = require("./delete_article")
const delete_comment = require("./delete_comment")
const db = require("../../models/index")
exports.create = async (obj) => {
    try {
        const data = {};
        data[`${obj.key}`] = obj.value;
        const users = await db.model_User_F.findAll({
            where:data,
            attributes:["id","avatar"]
        }
        )
        if(users.length){
            const author = {
                key:"author_id",
                value:users[0].id
            }
            const authors = {
                author_id:users[0].id
            }
            const article = await db.model_article_F.findAll({
                where:authors,
                attributes:["id","image"]
            })
            for (let index = 0; index < article.length; index++) {
                const comment = {
                    key:"blog_id",
                    value:article[index].id
                }
                console.log("查找用户评论："+comment)
                await delete_comment.create(comment)
            }
            await delete_comment.create({
                key:"user_id",
                value:users[0].id
            })
            await delete_article.create(author)
            await deleteImage.create(users[0].avatar)
            await db.model_User_F.destroy({
                where:data,
            })
        }
        return true
    } catch (error) {
        console.log(error)
        return error
    }
}